package com.dai.spec.gxk.controller;

import com.dai.spec.common.annotation.Log;
import com.dai.spec.common.core.controller.BaseController;
import com.dai.spec.common.core.domain.AjaxResult;
import com.dai.spec.common.core.page.TableDataInfo;
import com.dai.spec.common.enums.BusinessType;
import com.dai.spec.common.enums.ProfessionalTitleType;
import com.dai.spec.common.enums.SexType;
import com.dai.spec.common.utils.poi.ExcelUtil;
import com.dai.spec.gxk.domain.Teacher;
import com.dai.spec.gxk.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 教师信息管理Controller
 *
 * @author daikl
 * @date 2024-02-29
 */
@SuppressWarnings("all")
@Api("教师管理")
@RestController
@RequestMapping("/gxk/teacher")
public class TeacherController extends BaseController {
    @Autowired
    private TeacherService teacherService;

    /**
     * 查询教师信息管理列表
     */
    @ApiOperation("查询教师列表")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:list')")
    @GetMapping("/list")
    public TableDataInfo list(Teacher teacher) {
        startPage();
        List<Teacher> list = teacherService.queryList(teacher);
        return getDataTable(list);
    }

    /**
     * 导出教师信息管理列表
     */
    @ApiOperation("导出教师列表")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:export')")
    @Log(title = "教师信息管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Teacher teacher) {
        List<Teacher> list = teacherService.queryList(teacher);
        List<Teacher> teacherList = this.coverTeacher(list);
        ExcelUtil<Teacher> util = new ExcelUtil<Teacher>(Teacher.class);
        util.exportExcel(response, teacherList, "教师信息管理数据");
    }

    private static List<Teacher> coverTeacher(List<Teacher> list) {
        List<Teacher> coverTeacherList = list.stream().map(item -> {
            item.setSex(SexType.getValueByCode(item.getSex()));
            item.setProfessionalTitle(ProfessionalTitleType.getValueByCode(item.getProfessionalTitle()));
            return item;
        }).collect(Collectors.toList());
        return coverTeacherList;
    }

    /**
     * 获取教师详细信息
     */
    @ApiOperation("获取教师详细信息")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(teacherService.getById(id));
    }

    /**
     * 新增教师信息管理
     */
    @ApiOperation("新增教师")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:add')")
    @Log(title = "教师信息管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Teacher teacher) {
        return toAjax(teacherService.saveWithUser(teacher));
    }

    /**
     * 修改教师信息管理
     */
    @ApiOperation("修改教师")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:edit')")
    @Log(title = "教师信息管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Teacher teacher) {
        return toAjax(teacherService.updateWithUser(teacher));
    }

    /**
     * 删除教师信息管理
     */
    @ApiOperation("删除教师")
    @PreAuthorize("@ss.hasPermi('gxk:teacher:remove')")
    @Log(title = "教师信息管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(teacherService.removeByIdsWithUser(Arrays.asList(ids)));
    }
}
